import App from '../App.vue'
import LoginDouBao from '../views/LoginDoubao.vue'
import DataBind from '../views/DataBind.vue'
import LoginApp from '../views/LoginApp.vue'
import HaloView from '../views/HaloView.vue'
import doubaoChat from '../views/doubaoChat.vue'
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
        path: '/doubaoChat',
        name: 'doubaoChat',
        component: doubaoChat
    },
    {
        path: '/LoginDouBao',
        name: 'LoginDouBao',
        component: LoginDouBao
    },
    {
        path: '/DataBind',
        name: 'DataBind',
        component: DataBind
    },
    {
        path: '/LoginApp',
        name: 'LoginApp',
        component: LoginApp
    },
    {
        path: '/demo',
        name: 'demo',
        component: App
    },
    {
        path: '/HaloView',
        name: 'HaloView',
        component: HaloView
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    // 判断是否已登陆，sessionStorage.getItem('showLogin') === '0'代表了已登录
    // 已登录：isAuthenticated==true，未登录：isAuthenticated==false
    const isAuthenticated = sessionStorage.getItem('showLogin') === '0'
    // 判断目标路由是否为 /LoginDouBao
    if (to.path === '/LoginDouBao') {
        next() // 直接放行
    } else if (!isAuthenticated) {
        next({ path: '/LoginDouBao' })
    } else {
        next()
    }
})


export default router
















// import { createRouter, createWebHistory } from 'vue-router'

// function generateRoutesFromFiles() {
//     const viewsPath = require.context('../views', true, /\.vue$/)
//     const routes = viewsPath.keys().map(filePath => {
//         const routePath = filePath.replace('./', '').replace('.vue', '')
//         const componentName = routePath.split('/').pop()

//         return {
//             path: `/${routePath}`,
//             name: componentName,
//             component: () => import(`../views/${routePath}.vue`)
//         }
//     })

//     return routes
// }

// const routes = generateRoutesFromFiles()

// routes.push({
//     path: '/',
//     redirect: '/LoginDouBao'
// })
// const router = createRouter({
//     history: createWebHistory(process.env.BASE_URL),
//     routes
// })

// router.beforeEach((to, from, next) => {
//     const isAuthenticated = sessionStorage.getItem('showLogin') === '0'

//     if (!isAuthenticated) {
//         next({ name: '/LoginDouBao' })
//     } else {
//         next()
//     }
// })

// export default router